package com.windea.study.kotlin.game.tankwar2.model

import com.windea.study.kotlin.game.tankwar2.core.*
import com.windea.study.kotlin.game.tankwar2.enums.*
import com.windea.study.kotlin.game.tankwar2.interfaces.*

/**敌方坦克。*/
class EnemyTank(
	override var x: Int,
	override var y: Int
) : Tank(x, y), AutoMovable, AutoAttackable {
	override var direction: Direction = Direction.Down
	override val speed: Int = 8
	override var health: Int = 3
	
	private var lastShotTime = 0L
	private var shotFrequency = 800
	
	private var lastMoveTime = 0L
	private var moveFrequency = 30
	
	override fun draw() {
		val imagePath = when(direction) {
			Direction.Up -> "img/enemy_1_u.gif"
			Direction.Down -> "img/enemy_1_d.gif"
			Direction.Left -> "img/enemy_1_l.gif"
			Direction.Right -> "img/enemy_1_r.gif"
		}
		Painter.drawImage(imagePath, x, y)
	}
	
	override fun onAutoAttack(entities: MutableList<Entity>) {
		//频率检测
		val current = System.currentTimeMillis()
		if(current - lastShotTime < shotFrequency) return
		lastShotTime = current
		
		entities += Bullet(this, direction)
	}
	
	override fun onAutoMove(entities: MutableList<Entity>) {
		move(direction, entities)
	}
	
	override fun move(nextDirection: Direction, entities: MutableList<Entity>) {
		//频率检测
		val current = System.currentTimeMillis()
		if(current - lastMoveTime < moveFrequency) return
		lastMoveTime = current
		
		//判断是否是往要碰撞的方向走
		if(direction == badDirection) {
			direction = Direction.random(badDirection)
			return
		}
		
		super.move(nextDirection, entities)
	}
}
